﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Advanced_Terrain
{
    public class Menu
    {
        public List<String> MenuItems = new List<string>();
        public List<String> PlayerItems = new List<string>();
        public List<String> CourseItems = new List<string>();
        public List<String> ThrowingMethods = new List<string>();
        public List<String> WiiMoteCalibration = new List<string>();
        public List<String> WiiMoteSearching = new List<string>();
        public string ErrorMessage = "";
        int PlayerNum;
        List<String> CurrentList;
        String SelectedItem = "Start";
        int MenuID = 0;
        int ItemIndex = 0;
        SpriteFont Font;
        int FontHeight;
        SpriteBatch MySprites;
        float CurrentY = 0;
        float CurrentX = 400;
        Color MenuColor = Color.AliceBlue;
        Color SelectedColor = Color.Red;
        Color TitleColor = Color.AliceBlue;
        public Texture2D Background;
        public void Draw()
        {
            
            MySprites.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            MySprites.Draw(Background, new Vector2(0, 0), Color.White);
            string MenuTitle = "";
            switch (MenuID)
            {
                case 0:
                    
                    CurrentList = MenuItems;
                    if (ErrorMessage != "")
                        MenuTitle = ErrorMessage;
                    else
                        MenuTitle = "Main Menu";
                    break;
                case 1:
                    CurrentList = PlayerItems;
                    MenuTitle = "Select Number of Players";
                    break;
                case 2:
                    MenuTitle = "Select Course";
                    CurrentList = CourseItems;
                    break;
                case 3:
                    MenuTitle = Game1.Players[PlayerNum].Name + " select primary style (20% penalty to secondary)";
                    CurrentList = ThrowingMethods;
                    break;
                case 4:
                    MenuTitle = "Searching for WiiMote";
                    CurrentList = WiiMoteSearching;
                    break;
                case 5:
                    MenuTitle = "Calibrating WiiMote please wait";
                    CurrentList = WiiMoteCalibration;
                    break;
            }
            MySprites.DrawString(Font, MenuTitle, new Vector2(CurrentX - ((Font.MeasureString(MenuTitle).X * 1.5f) / 2 - 2), CurrentY), TitleColor, 0, new Vector2(0, 0), 1.5f, SpriteEffects.None, 0);
            CurrentY += 4 * FontHeight;
            {
                foreach (String Item in CurrentList)
                {
                    if (SelectedItem == Item)
                        MySprites.DrawString(Font, Item, new Vector2(CurrentX - (Font.MeasureString(Item).X / 2 -2), CurrentY), SelectedColor, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0);
                    MySprites.DrawString(Font, Item, new Vector2(CurrentX - (Font.MeasureString(Item).X / 2), CurrentY), MenuColor);
                    CurrentY += 3 * FontHeight;
                    
                 
                }
                MySprites.End();
                CurrentY = 50;
            }
        }
        public void SetSpriteBatch(GraphicsDevice device)
        {
            MySprites = new SpriteBatch(device);
        }
        public void SetFont(SpriteFont font)
        {
            Font = font;
            FontHeight = (int)Font.MeasureString("A").X;
        }
        public void Update()
        {
            
               
            
            
        }

        public void NextItem()
        {

            switch (MenuID)
            {
                case 0:
                    if (ItemIndex < MenuItems.Count - 1)
                        ItemIndex++;
                    SelectedItem = MenuItems[ItemIndex];
                    break;
                case 1:
                    if (ItemIndex < PlayerItems.Count - 1)
                        ItemIndex++;
                    SelectedItem = PlayerItems[ItemIndex];
                    break;
                case 2:
                    if (ItemIndex < CourseItems.Count - 1)
                        ItemIndex++;
                    SelectedItem = CourseItems[ItemIndex];
                    break;
                case 3:
                    if (ItemIndex < ThrowingMethods.Count - 1)
                        ItemIndex++;
                    SelectedItem = ThrowingMethods[ItemIndex];
                    break;
            }
        }
        public void PrevItem()
        {
            if (ItemIndex > 0)
                ItemIndex--;
            switch (MenuID)
            {
                case 0:
                    SelectedItem = MenuItems[ItemIndex];
                    break;
                case 1:
                    SelectedItem = PlayerItems[ItemIndex];
                    break;
                case 2:
                    SelectedItem = CourseItems[ItemIndex];
                    break;
                case 3:
                    SelectedItem = ThrowingMethods[ItemIndex];
                    break;
            }
        }
        public void SelectCurrent()
        {
            switch(MenuID)
            {
                case 0:
                    {
                        switch (SelectedItem)
                        {
                            case "Start":
                                SwitchMenu(2);
                                break;
                            case "Connect Motion++":
                                SwitchMenu(4);
                                Boolean Test;
                                Test = Game1.MyKeyboard.InitWiimote();
                                
                                break;
                            
                                
                        }
                    }
                    break;
                case 1:
                    {
                        switch (SelectedItem)
                        {
                            case "Back":
                                SwitchMenu(2);
                                break;
                            default:
                                {
                                    Game1.NumberOfPlayers = Convert.ToInt32(SelectedItem);
                                    for(int x = 0;x<Game1.NumberOfPlayers;x++)
                                    {
                                        Game1.Players[x] = new Player();
                                        Game1.Players[x].Name = "Player " + Convert.ToString(x+1);
                                        Game1.Players[x].IsActive = true;
                                    }
                                    SwitchMenu(3);
                                    PlayerNum = 0;
                                    //Game1.GameState = Game1.GameStateMenuPlayers;
                                    break;
                                }
                        }
                    }
                    break;
                case 2:
                    {
                        switch (SelectedItem)
                        {
                            case "Back":
                                SwitchMenu(0);
                                break;
                            default:
                                {
                                    string [] split = SelectedItem.Split(new char [] {','});
                                    Game1.CoursePath = Convert.ToString(split[0]);
                                    if (Convert.ToString(split[0]) == "Custom Course")
                                    {
                                        Game1.CoursePath = "c:\\DGCourse\\";
                                        Game1.EndofCourseName = ".bmp";
                                    }
                                    else
                                        Game1.CoursePath = "Courses\\" + Convert.ToString(split[0]) + "\\";
                                    
                                    Game1.CourseHoles = Convert.ToInt32(split[2]);
                                    SwitchMenu(1);
                                    //Game1.GameState = Game1.GameStateLoadHole;
                                
                                    //MessageBox.Show(Convert.ToString(split[0]));
                                    break;
                                }

                        }
                        break;
                    }
                case 3:
                    {
                        switch (SelectedItem)
                        {
                            case "Right Hand Backhand":
                                Game1.Players[PlayerNum].ThrowStyle = 1;
                                break;
                            case "Right Hand Forehand":
                                Game1.Players[PlayerNum].ThrowStyle = -1;
                                break;
                            case "Left Hand Backhand":
                                Game1.Players[PlayerNum].ThrowStyle = -1;
                                break;
                            case "Left Hand Forehand":
                                Game1.Players[PlayerNum].ThrowStyle = 1;
                                break;
                        }
                        if (PlayerNum == Game1.NumberOfPlayers - 1)
                            Game1.GameState = Game1.GameStateLoadHole;
                        else
                            PlayerNum++;
                        break;
                    }
            }
        }

        public void SwitchMenu(int NewMenu)
        {

            MenuID = NewMenu;
            ItemIndex = 0;
            switch (MenuID)
            {
                case 0:
                    SelectedItem = MenuItems[ItemIndex];
                    break;
                case 1:
                    SelectedItem = PlayerItems[ItemIndex];
                    break;
                case 2:
                    SelectedItem = CourseItems[ItemIndex];
                    break;
                case 3:
                    SelectedItem = ThrowingMethods[ItemIndex];
                    break;
            }
        }

    }
}
